var localObj = window.location;
var basePath = localObj.protocol+"//"+localObj.host;
var modelAddModule=angular.module('userGroupAddApp', []);
    modelAddModule.controller('userGroupAddCtr',['$scope', '$http',function($scope, $http) {
		// 模型名称校验默认不显示
		$scope.checkUserAccount = false;
		
		// 点击保存按钮，新增一条数据
		$scope.saveUserGroup = function() {
			// form表单的验证 数据不对，return false
			var formValid = $scope.userGroupForm.$valid;
				 if (!$scope.groupName) {
					toastr.warning("组名称不可为空！");
					formValid = false;
					return false;
				}
			if (formValid) { // 表单通过验证后提交
				checkUserAccount(); // 校验属性名称是否重复
			}
		};

		function checkUserAccount() {
			var result = true;
			$http({
				url : basePath+'/user/sameGroupAccountValidate',
				method : 'POST',
				params : {
					'groupName' : $scope.groupName
				}
			}).success(function(data, header, config, status) {
				if(data.length==0){
					submitData();
				}else{
					$scope.checkUserAccount = true;
					return;
				}
			}).error(function(data, header, config, status) {
				// 处理响应失败
				result = false;
			});
		}
		// form表单提交方法 submit(若是校验都通过啦，数据便可以提交啦)
		function submitData() {
			
			$http({
				url : basePath+'/user/saveUserGroup',
				method : 'POST',
				params : {
					'groupName' : $scope.groupName
				}
			}).success(function(data, header, config, status) {
				toastr.success("操作成功");
				setTimeout(function() {
				  location.href = basePath+'/user/userGroup';
				}, 500);
			}).error(function(data, header, config, status) {
				toastr.error("调用错误！");
				setTimeout(function() {
					//location.href = basePath+'/system/sysUser/user_apply.jsp';
				}, 500);
			});
		}
		//  点击取消按钮，跳转到查询页面
		$scope.backUser = function() {
			location.href = basePath+'/user/userGroup';
		};
}]);
   